import axios, { AxiosInstance, AxiosResponse } from 'axios'

interface DataInstance {
  [key: string]: any
}

interface serviceInstance extends AxiosInstance {
  upload<T = any, R = AxiosResponse<T>>(url: string, data?: DataInstance): Promise<R>;
}

const service: AxiosInstance = axios.create({
  baseURL: '/api',
  timeout: 10000,
  withCredentials: true
})

service.interceptors.request.use(config => {
  // todo
  return config
})

service.interceptors.response.use(response => {
  // todo 
  return response
})

const service$1: serviceInstance = Object.assign(service, {
  async upload<T = any, R = AxiosResponse<T>>(url: string, data: DataInstance) {
    const formData = new FormData()

    if (data) {
      Object.keys(data).forEach(key => {
        formData.append(key, data[key])
      })
    }
    const result: Promise<R> = service.post(url, formData, {
      headers: {
        'Content-Type': 'multipart/form-data'
      }
    })

    return result
  }
})

export default service$1
